package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.AdNode;
import com.usian.PageResult;
import com.usian.mapper.TbContentCategoryMapper;
import com.usian.mapper.TbContentMapper;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import com.usian.pojo.TbContentCategoryExample;
import com.usian.pojo.TbContentExample;
import com.usian.redis.RedisClient;
import com.usian.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;

    @Autowired
    private TbContentMapper tbContentMapper;

    @Autowired
    private RedisClient redisClient;

    @Value("${AD_CATEGORY_ID}")
    private Long AD_CATEGORY_ID;

    @Value("${AD_HEIGHT}")
    private Integer AD_HEIGHT;

    @Value("${AD_WIDTH}")
    private Integer AD_WIDTH;

    @Value("${AD_HEIGHTB}")
    private Integer AD_HEIGHTB;

    @Value("${AD_WIDTHB}")
    private Integer AD_WIDTHB;

    @Value("${PORTAL_AD_KEY}")
    private String PORTAL_AD_KEY;


//    内容分类管理根据父节点查询子节点
    @Override
    public List<TbContentCategory> selectContentCategoryByParentId(Long id) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        List<TbContentCategory> categoryList = tbContentCategoryMapper.selectByExample(example);
        return categoryList;
    }

    @Override
    public Integer insertContentCategory(Long parentId, String name) {
//        添加内容分类
        TbContentCategory tbContentCategory = new TbContentCategory();
        tbContentCategory.setParentId(parentId);
        tbContentCategory.setName(name);
        tbContentCategory.setCreated(new Date());
        tbContentCategory.setUpdated(new Date());
        tbContentCategory.setStatus(1);
        tbContentCategory.setSortOrder(1);
        tbContentCategory.setIsParent(false);
        int insert = tbContentCategoryMapper.insert(tbContentCategory);

//        查看父节点是否是父节点，如果不是就改为父节点
        TbContentCategory tbContentCategory1 = tbContentCategoryMapper.selectByPrimaryKey(parentId);
//        判断当前节点是否为父节点
        if (!tbContentCategory1.getIsParent()){
            tbContentCategory1.setIsParent(true);
            tbContentCategory1.setUpdated(new Date());
            tbContentCategoryMapper.updateByPrimaryKey(tbContentCategory1);
        }
        return insert;
    }

//    删除内容分类管理数据
    @Override
    public Integer deleteContentCategoryById(Long categoryId) {
//        查看当前数据是不是父节点
        TbContentCategory tbContentCategory = tbContentCategoryMapper.selectByPrimaryKey(categoryId);
//        当前数据为父节点不能删除
        if (tbContentCategory.getIsParent()==true){
            return 0;
        }
//        不是父节点
        tbContentCategoryMapper.deleteByPrimaryKey(categoryId);
//        查看当前对象的兄弟节点
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = tbContentCategoryExample.createCriteria();
        criteria.andParentIdEqualTo(tbContentCategory.getParentId());
        List<TbContentCategory> contentCategoryList = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);
//        判断当前数据有没有兄弟节点，如果没有就将isparent改为false
        if (contentCategoryList.size()==0) {
            TbContentCategory contentCategory = new TbContentCategory();
            contentCategory.setIsParent(false);
            contentCategory.setUpdated(new Date());
            contentCategory.setId(tbContentCategory.getParentId());
            tbContentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
        }
        return 200;

    }

//    修改内容分类数据
    @Override
    public Integer updateContentCategory(Long id, String name) {
        TbContentCategory tbContentCategory = new TbContentCategory();
        tbContentCategory.setId(id);
        tbContentCategory.setName(name);
        tbContentCategory.setUpdated(new Date());
        int update = tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
        return update;
    }

//    根据分类id查询内容列表
    @Override
    public PageResult selectTbContentAllByCategoryId(Long categoryId, Integer page, Integer rows) {
        PageHelper.startPage(page,rows);
        TbContentExample tbContentExample = new TbContentExample();
        TbContentExample.Criteria criteria = tbContentExample.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<TbContent> contentList = tbContentMapper.selectByExample(tbContentExample);

        PageInfo<TbContent> pageInfo = new PageInfo(contentList);
        PageResult pageResult = new PageResult();
        pageResult.setPageIndex(pageInfo.getPageNum());
        pageResult.setTotalPage(pageInfo.getPages());
        pageResult.setResult(pageInfo.getList());
        return pageResult;
    }

//    增加分类内容
    @Override
    public Integer insertTbContent(TbContent tbContent) {
        tbContent.setUpdated(new Date());
        tbContent.setCreated(new Date());
        redisClient.hdel(PORTAL_AD_KEY, AD_CATEGORY_ID.toString());
        int selective = tbContentMapper.insertSelective(tbContent);
        return selective;
    }

//    删除分类内容
    @Override
    public Integer deleteContentByIds(Long ids) {
        redisClient.hdel(PORTAL_AD_KEY, AD_CATEGORY_ID.toString());
        int key = tbContentMapper.deleteByPrimaryKey(ids);
        return key;
    }

    @Override
    public List<AdNode> selectFrontendContentByAD() {
//        先到redis中查没用在访问数据库
        ArrayList<AdNode> adNodeList1 = (ArrayList<AdNode>) redisClient.hget(PORTAL_AD_KEY, AD_CATEGORY_ID.toString());
        if (adNodeList1!=null && adNodeList1.size()>0){
            return adNodeList1;
        }

//        查询id=89的大广告位信息
        TbContentExample contentExample = new TbContentExample();
        TbContentExample.Criteria criteria = contentExample.createCriteria();
        criteria.andCategoryIdEqualTo(AD_CATEGORY_ID);
        List<TbContent> contentList = tbContentMapper.selectByExample(contentExample);
//        遍历list集合  赋值  adnode  add  list
        ArrayList<AdNode> adNodeList = new ArrayList<>();
        if (contentList.size()>0) {
            for (TbContent tbContent : contentList) {
                AdNode adNode = new AdNode();
                adNode.setSrc(tbContent.getPic());
                adNode.setSrcB(tbContent.getPic2());
                adNode.setHref(tbContent.getUrl());
                adNode.setHeight(AD_HEIGHT);
                adNode.setWidth(AD_WIDTH);
                adNode.setHeightB(AD_HEIGHTB);
                adNode.setWidthB(AD_WIDTHB);
                adNodeList.add(adNode);
            }
        }
//        首页广告缓存到redis使用hash类型
        redisClient.hset(PORTAL_AD_KEY,AD_CATEGORY_ID.toString(),adNodeList);
        return adNodeList;
    }
}
